import {
	config
} from '../config/index.js'
import privacyCheck from '@/components/lq-privacy-dialog/privacyCheck.js'
export const login = (loginSuccess) => {
	privacyCheck({
		//已同意
		agree:()=>{
			uni.showModal({
				title: '登录',
				content: '是否授权微信登录',
				success: function(result) {
					if (result.confirm) {
						handlerLogin(loginSuccess);
					}
				},
			})
		},
		//不同意
		disAgree:()=>{
			console.log('不同意')
		}
	})
	

}

export const handlerLogin = (loginSuccess) => {
	uni.login({
		provider: 'weixin',
		success(resp) {
			uni.request({
				url: `/admin/wx/login/weapp`,
				method: 'POST',
				data: {
					code: resp.code
				},
				success(res) {
					
					uni.setStorageSync("token", res.data.data)
					getUserInfo().then(userResp=>{
						uni.setStorageSync("userInfo", userResp)
						if (userResp.nickName === null || userResp.phone === null) {
							uni.showModal({
								title: "登录提示",
								content: "当前用户信息未填写完成，前往填写用户信息?",
								success() {
									uni.navigateTo({
										url: '/pages/userInfo/userInfo'
									})
								}
							})
						}
						loginSuccess(userResp.data);
					})

					
				},
				fail() {
					uni.showToast({
						icon: 'error',
						title: "登录失败，请稍后重试"
					})
				}
			})
		}
	})


}

export const updateUserInfo = (data) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `/admin/wx/login/updateUserInfo`,
			method: 'POST',
			data,
			success(resp) {
				uni.showToast({
					icon: 'success',
					title: "修改成功"
				})
				resolve(resp.data.data)
			},
			fail(e) {
				reject(new Error('请求失败'));
			}
		})
	})

}

export const getUserInfo = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `/admin/wx/login/userInfo`,
			method: 'GET',
			success(resp) {

				uni.setStorageSync("userInfo", resp.data.data)
				resolve(resp.data.data)
			},
			fail(e) {
				reject(new Error('请求失败'));
			}
		})
	})

}

export const listSchool = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `/admin/wx/student/listSchool`,
			method: 'GET',
			success(resp) {
				resolve(resp.data.data)
			},
			fail(e) {
				reject(new Error('请求失败'));
			}
		})
	})

}

export const listGrade = (params) => {
	return new Promise((resolve, reject) => {
		// 构建查询字符串
		const queryString = params ? Object.keys(params)
			.filter(key => params[key] !== null && params[key] !== undefined)
			.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
			.join('&') : '';
		const url = queryString ? `/admin/wx/student/listGrade?${queryString}` : `/admin/wx/student/listGrade`;

		uni.request({
			url,
			method: 'GET',
			success(resp) {
				resolve(resp.data.data)
			},
			fail(e) {
				reject(new Error('请求失败'));
			}
		})
	})

}

export const listClasses = (params) => {
	return new Promise((resolve, reject) => {
		// 构建查询字符串
		const queryString = params ? Object.keys(params)
			.filter(key => params[key] !== null && params[key] !== undefined)
			.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
			.join('&') : '';
		const url = queryString ? `/admin/wx/student/listClasses?${queryString}` : `/admin/wx/student/listClasses`;

		uni.request({
			url,
			method: 'GET',
			success(resp) {
				resolve(resp.data.data)
			},
			fail(e) {
				reject(new Error('请求失败'));
			}
		})
	})

}